import { reactive } from 'vue'
import type { FormRules } from 'element-plus'
import { $t, transformI18n } from '@/plugins/i18n'
import { useUserStoreHook } from '@/store/modules/user'
/** 6位数字验证码正则 */
export const REGEXP_SIX = /^\d{6}$/

/** 密码正则（密码格式应为8-16位数字、字母、符号的任意两种组合） */
export const REGEXP_PWD =
  /^(?!^[a-zA-Z]*$)(?!^[0-9]*$)(?!^[!@#$%^&*(),.?":{}|<>]*$).{6,16}$/

/** 登录校验 */
const loginRules = reactive<FormRules>({
  password: [
    {
      validator: (rule, value, callback) => {
        if (value === '') {
          callback(new Error(transformI18n($t('login.password'))))
        } else if (!REGEXP_PWD.test(value)) {
          callback(new Error(transformI18n($t('login.passwordReg'))))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ],
  verifyCode: [
    {
      validator: (rule, value, callback) => {
        if (value === '') {
          callback(new Error(transformI18n($t('login.verficationEmpty'))))
        } else if (useUserStoreHook().verificationCode !== value) {
          callback(new Error(transformI18n($t('login.verficationEmptyReg'))))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ]
})
export { loginRules }
